package com.zj.osm.view.project.addproject;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.zj.osm.R;
import com.zj.osm.entity.greendao.Project;
import com.zj.osm.model.pojo.Equip;
import com.zj.osm.model.pojo.Module;
import com.zj.osm.model.pojo.ModuleType;
import com.zj.osm.mvp.MvpActivity;
import com.zj.osm.utils.db_utils.EquipUtils;
import com.zj.osm.utils.db_utils.ModuleTypeUtils;
import com.zj.osm.utils.db_utils.ModuleUtils;
import com.zj.osm.view.map.map.MapActivity;
import com.zj.utils.data.LiveDataBus;
import com.zj.utils.utils.utilcode.KeyboardUtils;
import com.zj.utils.utils.utilcode.ToastUtils;
import com.zj.utils.view.TitleBar;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.zj.osm.utils.Configuration.PROJECT_ID;


/**
 * 添加新项目的活动
 */
public class AddProjectActivity extends MvpActivity<AddProjectActivityView, AddProjectActivityPresenter> implements View.OnClickListener, AddProjectActivityView {
    private EditText mPtNameEt;
    private EditText mPtNoteEt;
    private Spinner mPtVoltageSr;//电压等级
    private Spinner mPtNewTySr;//新建类型
    private TextView mLeverTv;//杆型
    private TextView mPtOverHeaderTv;//架空线路
    private TextView mPtCableTv;//电缆线路
    private TitleBar mSureTr;
    private Spinner lineSpLaying;
    private RelativeLayout lineRlLaying;
    private String ganxinModuleTypeId = "";
    String ganxinModuleId = "";
    ArrayList<Module> gangaoArray = new ArrayList<>();
    private String ganxinModuleName = "";
    private String jiakongEquipId = "";
    private String jiakongModuleId = "";
    private String dilanEquipId = "";
    private String dilanModuleId = "";
    Project project;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_project);
        KeyboardUtils.fixAndroidBug5497(this);
        initView();
    }

    @Override
    public AddProjectActivityPresenter createPresenter() {
        return new AddProjectActivityPresenter();
    }

    @Override
    public AddProjectActivityView createView() {
        return this;
    }

    private void initView() {
        mPtNameEt = findViewById(R.id.add_ed_name);
        mPtNoteEt = findViewById(R.id.add_ed_note);
        mPtVoltageSr = findViewById(R.id.add_sp_dengji);
        mPtNewTySr = findViewById(R.id.add_sp_leixing);
        mLeverTv = findViewById(R.id.xuanxingCenterTv);
        mLeverTv.setOnClickListener(this);
        mPtOverHeaderTv = findViewById(R.id.jiakongTv);
        mPtOverHeaderTv.setOnClickListener(this);
        mPtCableTv = findViewById(R.id.dianlanTv);
        mPtCableTv.setOnClickListener(this);
        findViewById(R.id.add_btn_cancels).setOnClickListener(this);
        findViewById(R.id.add_btn_save).setOnClickListener(this);
        mSureTr = findViewById(R.id.add_item_tr);
        mSureTr.setTitle(getString(R.string.add_item_tiltle));
//        mSureTr.setSaveEnabled(true);
        mSureTr.setBackImageVisiable(true);
//        mSureTr.setRightText(getString(R.string.map_dianjiwancheng));
//        mSureTr.setRightTextOnClickListener(this);
        findViewById(R.id.txtRight).setPadding(0, 0, 20, 0);

    }

    @Override
    protected void onResume() {

        super.onResume();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_btn_cancels:
                finish();
                break;
            case R.id.add_btn_save:
                submit();
                break;
            case R.id.txtRight:

                break;
            case R.id.xuanxingCenterTv://杆型
                showXuanxingDialog(1);
                break;
            case R.id.jiakongTv: //架空线路
                showXuanxingDialog(2);
                break;
            case R.id.dianlanTv://电缆
                showXuanxingDialog(3);
                break;
        }

    }

    private void showXuanxingDialog(int flag) {
        final View views = View.inflate(this, R.layout.activity_program_dialog, null);
        final Spinner map_ed_levers = views.findViewById(R.id.map_ed_lever);
        final Spinner map_ed_head = views.findViewById(R.id.map_ed_head);
        final Spinner map_ed_shaojing = views.findViewById(R.id.map_ed_shaojing);
        final Spinner map_ed_ganxing = views.findViewById(R.id.map_ed_ganxings);
        final LinearLayout gangao_line = views.findViewById(R.id.gangao_line);
        final TextView xinluLiebie = views.findViewById(R.id.xianluLiebbie);
        final TextView xinluLieXing = views.findViewById(R.id.xianluLieXing);
        lineSpLaying = views.findViewById(R.id.lineSpLaying);
        lineRlLaying = views.findViewById(R.id.lineRlLaying);
        if (flag == 1) {
            gangao_line.setVisibility(View.VISIBLE);
        }
        Button map_btn_quxiao = views.findViewById(R.id.map_btn_quxiao);
        Button map_btn_queding = views.findViewById(R.id.map_btn_queding);
        AlertDialog dialogFushu = new AlertDialog.Builder(this).create();

        map_btn_quxiao.setOnClickListener(v -> {
            dialogFushu.dismiss();

        });
        map_btn_queding.setOnClickListener(v -> {
            switch (flag) {
                case 1://杆型
                    ModuleType moduleType = (ModuleType) map_ed_levers.getSelectedItem();
                    Module module = (Module) map_ed_ganxing.getSelectedItem();
                    mLeverTv.setText(module.getModuleName());
                    ganxinModuleTypeId = moduleType.getModuleTypeID();
                    ganxinModuleId = module.getModuleID();
                    ganxinModuleName = module.getModuleName();
                    break;
                case 2:
                    Equip equip = (Equip) map_ed_levers.getSelectedItem();
                    Module module2 = (Module) map_ed_ganxing.getSelectedItem();
                    jiakongEquipId = equip.getId() + "";
                    jiakongModuleId = module2.getModuleID();
                    mPtOverHeaderTv.setText(module2.toString());
                    break;
                case 3:
                    Equip equip3 = (Equip) map_ed_levers.getSelectedItem();
                    Module module3 = (Module) map_ed_ganxing.getSelectedItem();
                    dilanEquipId = equip3.getId() + "";
                    dilanModuleId = module3.getModuleID();
                    mPtCableTv.setText(module3.toString());
                    break;
            }
            dialogFushu.dismiss();
        });
        switch (flag) {
            case 1://杆型
                //类型
                lineRlLaying.setVisibility(View.GONE);
                //List<ModuleType> moduleTypeList = getPresenter().getModuleType();
                List<ModuleType> moduleTypeList = ModuleTypeUtils.getInstance().getModuleType();
                //适配器
                ArrayAdapter<ModuleType> arr_adapter21 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, moduleTypeList);
                arr_adapter21.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                map_ed_levers.setAdapter(arr_adapter21);
                if (!ganxinModuleTypeId.isEmpty()) {
                    for (int i = 0; i < moduleTypeList.size(); i++) {
                        if (moduleTypeList.get(i).getModuleTypeID().contains(ganxinModuleTypeId)) {
                            map_ed_levers.setSelection(i);
                        }
                    }
                }

                map_ed_levers.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        ModuleType typeClass = moduleTypeList.get(position);
                        //获取杆高这些的类型。
                        gangaoArray = ModuleUtils.getInstance().getGangaoResult(typeClass.getModuleTypeKey());
                        ///最终复制的内容。
                        ArrayAdapter<Module> ganxinAdapter = new ArrayAdapter<Module>(AddProjectActivity.this, android.R.layout.simple_spinner_item, gangaoArray);
                        ganxinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        map_ed_ganxing.setAdapter(ganxinAdapter);
                        for (int i = 0; i < gangaoArray.size(); i++) {
                            if (gangaoArray.get(i).getModuleID().contains(ganxinModuleId) && ganxinModuleId.length() > 1) {
                                map_ed_ganxing.setSelection(i);
                                break;
                            }
                        }

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });

                map_ed_head.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        ArrayList<Module> gangaoNew = new ArrayList<>();
                        for (Module moduleInser : gangaoArray) {
                            if ((moduleInser.getModuleName().contains(map_ed_head.getSelectedItem().toString()) || map_ed_head.getSelectedItem().toString().equals("全部"))
                                    && (moduleInser.getModuleName().contains(map_ed_shaojing.getSelectedItem().toString()) || map_ed_shaojing.getSelectedItem().toString().equals("全部"))) {
                                gangaoNew.add(moduleInser);
                            }
                        }
                        ArrayAdapter<Module> ganxinAdapter = new ArrayAdapter<Module>(AddProjectActivity.this, android.R.layout.simple_spinner_item, gangaoNew);
                        ganxinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        map_ed_ganxing.setAdapter(ganxinAdapter);
                        for (int i = 0; i < gangaoNew.size(); i++) {
                            if (gangaoNew.get(i).getModuleName().equals(ganxinModuleName) && ganxinModuleName.length() > 1) {
                                map_ed_ganxing.setSelection(i);
                                break;
                            }
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });

                map_ed_shaojing.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        ArrayList<Module> gangaoNew = new ArrayList<>();
                        for (Module moduleInser : gangaoArray) {
                            if ((moduleInser.getModuleName().contains(map_ed_head.getSelectedItem().toString()) || map_ed_head.getSelectedItem().toString().equals("全部"))
                                    && (moduleInser.getModuleName().contains(map_ed_shaojing.getSelectedItem().toString()) || map_ed_shaojing.getSelectedItem().toString().equals("全部"))) {
                                gangaoNew.add(moduleInser);
                            }
                        }

                        ArrayAdapter<Module> ganxinAdapter = new ArrayAdapter<Module>(AddProjectActivity.this, android.R.layout.simple_spinner_item, gangaoNew);
                        ganxinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        map_ed_ganxing.setAdapter(ganxinAdapter);
                        for (int i = 0; i < gangaoNew.size(); i++) {
                            if (gangaoNew.get(i).getModuleName().equals(ganxinModuleName) && ganxinModuleName.length() > 1) {
                                map_ed_ganxing.setSelection(i);
                                break;
                            }
                        }

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });


                break;
            case 2: {
                lineRlLaying.setVisibility(View.GONE);
                xinluLiebie.setText("类别");
                xinluLieXing.setText("线路");
                List<Equip> equipList = EquipUtils.getInstance().getEquip(3, "架空");
                //适配器
                ArrayAdapter<Equip> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, equipList);
                //设置样式
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                map_ed_levers.setAdapter(adapter);
                if (!jiakongEquipId.isEmpty()) {
                    for (int i = 0; i < equipList.size(); i++) {
                        if ((equipList.get(i).getId() + "").contains(jiakongEquipId)) {
                            map_ed_levers.setSelection(i);
                        }
                    }
                }
                map_ed_levers.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        Equip equip = ((Equip) map_ed_levers.getSelectedItem());
                        List<Module> modulesList = ModuleUtils.getInstance().getModuleList(equip.getId());
                        //适配器
                        ArrayAdapter<Module> arr_adapter4 = new ArrayAdapter<>(AddProjectActivity.this, android.R.layout.simple_spinner_item, modulesList);
                        arr_adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        map_ed_ganxing.setAdapter(arr_adapter4);
                        for (int i = 0; i < modulesList.size(); i++) {
                            if (modulesList.get(i).getModuleID().contains(jiakongModuleId) && jiakongModuleId.length() > 1) {
                                map_ed_ganxing.setSelection(i);
                                break;
                            }
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
            }

            break;
            case 3: {
                  /*
                //电缆直埋
                private String path;*/
                lineRlLaying.setVisibility(View.VISIBLE);
                xinluLiebie.setText("类别");
                xinluLieXing.setText("线路");
                List<Equip> equipList = EquipUtils.getInstance().getEquip(3, "电缆");
                //适配器
                ArrayAdapter<Equip> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, equipList);
                //设置样式
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                map_ed_levers.setAdapter(adapter);
                if (!dilanEquipId.isEmpty()) {
                    for (int i = 0; i < equipList.size(); i++) {
                        if ((equipList.get(i).getId() + "").contains(dilanEquipId)) {
                            map_ed_levers.setSelection(i);
                        }
                    }
                }
                map_ed_levers.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        Equip equip = ((Equip) map_ed_levers.getSelectedItem());
                        List<Module> modulesList = ModuleUtils.getInstance().getModuleList(equip.getId());
                        //适配器
                        ArrayAdapter<Module> arr_adapter4 = new ArrayAdapter<>(AddProjectActivity.this, android.R.layout.simple_spinner_item, modulesList);
                        arr_adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        map_ed_ganxing.setAdapter(arr_adapter4);
                        for (int i = 0; i < modulesList.size(); i++) {
                            if (modulesList.get(i).getModuleID().contains(dilanModuleId) && dilanModuleId.length() > 1) {
                                map_ed_ganxing.setSelection(i);
                                break;
                            }
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
            }
            break;
        }
        dialogFushu.setView(views);
        dialogFushu.show();

    }

    /**
     * 提交
     */
    private void submit() {
        KeyboardUtils.hideSoftInput(this);
        String name = mPtNameEt.getText().toString().trim();//获取输入的项目名称
        String voltage = mPtVoltageSr.getSelectedItem().toString().trim();//获取电压等级
        String newType = mPtNewTySr.getSelectedItem().toString().trim();//获取建设类型
        String note = mPtNoteEt.getText().toString().trim();//获取输入的备注信息
        String date = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        String projectId = UUID.randomUUID() + "";
        //判断输入信息是否为空
        if (name.isEmpty()) {
            ToastUtils.showShort("请输入完整信息");
        } else {
            if (name.length() > 50) {
                ToastUtils.showShort("请输入50个以下字符");
            } else {
                project=new Project();
                project.setName(name);
                project.setId(null);
                project.setNote(note);
                project.setProjectId(projectId);
                project.setIsCheck(true);
                project.setTime(date);
                project.setState("本地");
                project.setStop("0");
                project.setVoltage(voltage);
                project.setType(newType);
                ///储存 杆型 架高线路  电缆线路   id,id|id,id|id,id  根据  |  截取 然后 id,id   id,id  id,id 然后根据  , 截图
                String path = "";
                if (lineSpLaying != null) {
                    path = lineSpLaying.getSelectedItem().toString();
                }
                project.setGanxinModuleId(ganxinModuleId);
                project.setGanxinModuleTypeId(ganxinModuleTypeId);

                project.setJiakongEquipId(jiakongEquipId);
                project.setJiakongModuleId(jiakongModuleId);

                project.setDilanEquipId(dilanEquipId);
                project.setDilanModuleId(dilanModuleId);

                project.setPath(path);
                project.setUnit("");
                //这里调用P层同只Modle层。
                getPresenter().addProject(project);
            }
        }
    }

    /**
     * 添加项目回调
     *
     * @param isAddSuccess 添加项目是否成功
     */
    @Override
    public void addProject(boolean isAddSuccess) {
        if (isAddSuccess) {
            ToastUtils.showShort("创建成功");
            LiveDataBus.get().getChannel(PROJECT_ID).setValue(project.getId());
            startIntent(MapActivity.class);
            finish();
        } else {
            ToastUtils.showShort("新建项目失败");
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
